/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.st.config.Constant;
import com.jeesite.modules.st.entity.StSmallMaterialApproveHistory;
import com.jeesite.modules.st.pojo.AjaxJson;
import com.jeesite.modules.st.service.StSmallMaterialApproveHistoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.st.entity.StSmallMaterialApprove;
import com.jeesite.modules.st.service.StSmallMaterialApproveService;

import java.util.ArrayList;
import java.util.List;

/**
 * 小料间审核Controller
 *
 * @author HOHD
 * @version 2021-01-15
 */
@Controller
@RequestMapping(value = "${adminPath}/st/stSmallMaterialApprove")
public class StSmallMaterialApproveController extends BaseController {

    @Autowired
    private StSmallMaterialApproveService stSmallMaterialApproveService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public StSmallMaterialApprove get(String id, boolean isNewRecord) {
        return stSmallMaterialApproveService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("st:stSmallMaterialApprove:view")
    @RequestMapping(value = {"list", ""})
    public String list(StSmallMaterialApprove stSmallMaterialApprove, Model model) {
        model.addAttribute("stSmallMaterialApprove", stSmallMaterialApprove);
        return "modules/st/stSmallMaterialApproveList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("st:stSmallMaterialApprove:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<StSmallMaterialApprove> listData(StSmallMaterialApprove stSmallMaterialApprove, HttpServletRequest request, HttpServletResponse response) {
        stSmallMaterialApprove.setPage(new Page<>(request, response));
        Page<StSmallMaterialApprove> page = stSmallMaterialApproveService.findPage(stSmallMaterialApprove);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("st:stSmallMaterialApprove:view")
    @RequestMapping(value = "form")
    public String form(StSmallMaterialApprove stSmallMaterialApprove, Model model) {
        model.addAttribute("stSmallMaterialApprove", stSmallMaterialApprove);
        return "modules/st/stSmallMaterialApproveForm";
    }

    @Autowired
    StSmallMaterialApproveHistoryService stSmallMaterialApproveHistoryService;

    @RequestMapping(value = "approve")
    @ResponseBody
    public AjaxJson approve(String message) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        StSmallMaterialApprove query = new StSmallMaterialApprove();
        query.setTakeStatus(Constant.take_status.in_take);
        List<StSmallMaterialApprove> list = stSmallMaterialApproveService.findList(query);
        if (list.isEmpty()) {
            ajaxJson.code = -1;
            ajaxJson.data = "没有可以审核的东西";
            return ajaxJson;
        }
//        List<StSmallMaterialApproveHistory> saveList = new ArrayList<>();
        for (StSmallMaterialApprove stSmallMaterialApprove : list) {
            StSmallMaterialApproveHistory history = new StSmallMaterialApproveHistory();
            BeanUtils.copyProperties(stSmallMaterialApprove, history);
            history.setId(null);
//            saveList.add(history);
            stSmallMaterialApproveHistoryService.save(history);
        }
        stSmallMaterialApproveService.approve();
        return ajaxJson;
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("st:stSmallMaterialApprove:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated StSmallMaterialApprove stSmallMaterialApprove) {
        stSmallMaterialApproveService.save(stSmallMaterialApprove);
        return renderResult(Global.TRUE, text("保存小料间审核成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("st:stSmallMaterialApprove:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(StSmallMaterialApprove stSmallMaterialApprove) {
        stSmallMaterialApproveService.delete(stSmallMaterialApprove);
        return renderResult(Global.TRUE, text("删除小料间审核成功！"));
    }

}